package com.king.spark.rdd.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author wdl
 * @Date 2022/11/22 16:34
 */
object Spark_RDD_flatmap {

  def main(args: Array[String]): Unit = {

    // 创建Spark运行配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")

    // 创建Spark上下文环境对象（连接对象）t
    val sparkContext: SparkContext = new SparkContext(sparkConf)

//    val dataRDD = sparkContext.makeRDD(List(List(1, 2), List(3, 4)), 1)
    val dataRDD: RDD[Int] = sparkContext.makeRDD(List(1, 2, 3, 4), 2)

    val value2: RDD[Int] = dataRDD.flatMap(List(_: Int))

    val listRDD: RDD[Any] = sparkContext.makeRDD(List(List(1, 2), 3, List(4, 5)), 2)
    val value1: RDD[Any] = listRDD.flatMap(lists => {
      lists match {
        case a: List[_] => a
        case a: Int => List(a)
      }
    })
//    listRDD.flatMap {
//      case ele: List[_] => ele
//      case ele: Int => List(ele)
//    }.collect().foreach(println)


    val stringRDD: RDD[String] = sparkContext.makeRDD(List("Hello Spark", "Hello Scala", "Hello Spark", "Hello World"), 2)

    val value: RDD[String] = stringRDD.flatMap(List(_: String))
    value.collect().foreach(println)

    sparkContext.stop()
  }
}
